﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Manager.ascx.cs" Inherits="FreshCms.BackEnd.Controls.Product.Manager" %>

<script src="/BackEnd/js/jquery.price_format.min.js" type="text/javascript"></script>

<div id="page-content" class="block">
    <div class="block-header">
        <a href="javascript:void(0)" class="header-title-link">
            <h1>Quản lý sản phẩm</h1>
        </a>
    </div>

    <div class="col-sm-2"></div>
    <div class="col-sm-9">
        <div class="block">
            <div class="block-title"><h2>Chức năng</h2></div>
            <div class="col-sm-12">
                <div class="col-sm-6">
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-gift"></i></span>
                            <input type="text" id="txtName" class="form-control" value="" placeholder="(Ví dụ: Sting dâu)">
                            <span class="input-group-addon">Tên sản phẩm</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-barcode"></i></span>
                            <input type="text" id="txtPrice" class="form-control" value="" placeholder="(Ví dụ: 10.000)">
                            <span class="input-group-addon">Giá sản phẩm</span>
                        </div>
                    </div>
                    
                </div>
                <div class="col-sm-6">
                <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="gi gi-rotation_lock"></i></span>
                            <input type="text" id="txtDesc" class="form-control" value="" placeholder="(Ví dụ: Chai / Cốc / Điếu ...)">
                            <span class="input-group-addon">Đơn vị</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-sitemap"></i></span>
                            <input type="text" id="txtQuantity" class="form-control" value="" placeholder="(Ví dụ: 24)">
                            <span class="input-group-addon">Số lượng kho</span>
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="form-group text-center">
                <a href="javascript:void(0)" id="btnAdd" class="btn btn-sm btn-success"><i class="hi hi-plus mr5"></i>Thêm mới</a>
            </div>
        </div>
    </div>

    <div class="table-responsive">
        <table id="tblParent" class="table table-hover">
            <thead>
                <tr>
                    <th class="text-center" style="width: 60px;">#</th>
                    <th class="text-center" style="width: 60px;">Id</th>
                    <th>Tên sản phẩm</th>
                    <th>Đơn vị</th>
                    <th style="width: 100px;">Giá</th>
                    <th style="width: 110px;">SL kho</th>
                    <th class="text-center" style="width: 150px;">Thao tác</th>
                </tr>
            </thead>
            <tbody>
                <asp:Repeater ID="rpt" runat="server">
                    <ItemTemplate>
                        <tr id="trMain_<%# Eval("ProId") %>">
                            <td class="text-center"><%# Container.ItemIndex + 1 %></td>
                            <td class="text-center"><%# Eval("ProId")%></td>
                            <td><%# Eval("ProName")%></td>
                            <td><%# Eval("ProDesc")%></td>
                            <td rel="tdPrice"><%# Eval("ProPrice")%></td>
                            <td rel="tdQuantity"><%# Eval("ProTotalQuantity")%></td>
                            <td class="text-center">
                                <div class="btn-group">
                                    <a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-default"
                                        data-original-title="Sửa nhanh" onclick="activeExtTR(<%# Eval("ProId") %>)"><i class="gi gi-pen"></i></a>
                                    <div class="btn-group dropup text-left">
                                        <a href="javascript:void(0)" data-toggle="dropdown" class="btn btn-xs btn-default btn-danger dropdown-toggle"><i class="gi gi-bin"></i> </a>
                                        <ul class="dropdown-menu dropdown-menu-delete">
                                            <li><a href="javascript:void(0)" onclick="DeleteData(<%# Eval("ProId") %>, '<%#Server.HtmlEncode(Eval("ProName").ToString())%>')" data-toggle="tooltip" data-original-title="Sản phẩm xóa sẽ không thể phục hồi" data-placement="left">Xóa</a></li>
                                            <li><a href="javascript:void(0)">Hủy</a></li>
                                        </ul>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr id="trExt_<%# Eval("ProId") %>" style="display:none/*table-row*/;">
                            <td class="text-center"><%# Container.ItemIndex + 1 %></td>
                            <td class="text-center"><%# Eval("ProId")%></td>
                            <td><input type="text" id="txtName_<%# Eval("ProId") %>" class="form-control" value="<%# Eval("ProName") %>"></td>
                            <td><input type="text" id="txtDesc_<%# Eval("ProId") %>" class="form-control" value="<%# Eval("ProDesc") %>"></td>
                            <td><input type="text" id="txtPrice_<%# Eval("ProId") %>" class="form-control" value="<%# Eval("ProPrice") %>" rel="price"></td>
                            <td><input type="text" id="txtQuantity_<%# Eval("ProId") %>" class="form-control" value="<%# Eval("ProTotalQuantity") %>"></td>
                            
                            <td class="text-center">
                                <div class="btn-group">
                                    <a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-default btn-success"
                                        data-original-title="Hoàn tất" onclick="UpdateData(<%# Eval("ProId") %>)"><i class="hi hi-ok"></i></a>

                                    <a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-default btn-primary" 
                                        data-original-title="Hủy" onclick="activeMainTR(<%# Eval("ProId") %>)"><i class="hi hi-share-alt"></i></a>
                                </div>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </tbody>
        </table>
    </div>
    
</div>

<script>

    $(function () {
        $('#txtPrice').priceFormat({
            prefix: '',
            centsSeparator: ',',
            thousandsSeparator: '.',
			centsLimit: 0
        });
        $('#txtQuantity').priceFormat({
            prefix: '',
            centsSeparator: ',',
            thousandsSeparator: '.',
			centsLimit: 0
        });
        
        // format tiền cho cả list
        $("#tblParent input[rel*='price']").each(function () {
            $(this).priceFormat({
                prefix: '',
                centsSeparator: ',',
                thousandsSeparator: '.',
                centsLimit: 0
            });
        });
        // format tiền cho cả list
        $("#tblParent td[rel*='tdPrice']").each(function () {
            $(this).priceFormat({
                prefix: '',
                centsSeparator: ',',
                thousandsSeparator: '.',
                centsLimit: 0
            });
        });
    });

    $("#btnAdd").bind("click", function () {
        if (!chkTextBox('txtName', 'Bạn phải nhập Tên sản phẩm...')) return false;
        if (!chkTextBox('txtPrice', 'Bạn phải Giá Tiền sản phẩm...')) return false;
        if (!chkTextBox('txtDesc', 'Bạn phải nhập đơn vị đo...')) return false;
        if (!chkTextBox('txtQuantity', 'Bạn phải nhập số lượng sản phẩm trong kho...')) return false;

        var name = $('#txtName').val();
        var quantity = $('#txtQuantity').val().replace(/\./g, '');
        var price = $('#txtPrice').val().replace(/\./g, '');
        var desc = $('#txtDesc').val();

        // call ajax
        var param;
        param = {
            m: "add",
            ui : <%=panelId %>,
            name: name,
            quantity: quantity,
            price: price,
            desc: desc
        };
        $.ajax({
            url: "/BackEnd/Handler/BlueProduct.ashx",
            data: param,
            type: "POST",
            dataType: "json",
            beforeSend: function () {
                jWaiting.callWaiting();
            },
            success: function (data) {
                //console.log(data);
                if (data.Success) {
                    jWaiting.callWaitingMessSuccess('Cập nhật sản phẩm thành công...', 'reload', true);
                }
                else {
                    jWaiting.callWaitingMessError('Có lỗi: ' + data.Message);
                }
            }
        });
    });

    // event Update Fast in data table
    function UpdateData(id) {
        var $row = $('#trExt_' + id);
        var name = $('#txtName_' + id).val();
        var desc = $('#txtDesc_' + id).val();
        var price = $('#txtPrice_' + id).val().replace(/\./g, '');
        var quantity = $('#txtQuantity_' + id).val().replace(/\./g, '');
        
        // call ajax
        var param;
        param = {
            m: "add",
            ui : <%=panelId %>,
            id: id,
            name: name,
            quantity: quantity,
            price: price,
            desc: desc
        };
        $.ajax({
            url: "/BackEnd/Handler/BlueProduct.ashx",
            data: param,
            type: "POST",
            dataType: "json",
            beforeSend: function () {
                jWaiting.callWaiting();
            },
            success: function (data) {
                //console.log(data);
                if (data.Success) {
                    jWaiting.callWaitingMessSuccess('Cập nhật sản phẩm thành công...', 'reload', true);
                }
                else {
                    jWaiting.callWaitingMessError('Có lỗi: ' + data.Message);
                }
            }
        });
    }

    // event Delete Fast in data table
    function DeleteData(id, proname) {
        // call ajax
        var param;
        param = {
            m: "delete",
            ui : <%=panelId %>,
            id: id, 
            name: proname
        };
        $.ajax({
            url: "/BackEnd/Handler/BlueProduct.ashx",
            data: param,
            type: "POST",
            dataType: "json",
            beforeSend: function () {
                jWaiting.callWaiting();
            },
            success: function (data) {
                //console.log(data);
                if (data.Success) {
                    jWaiting.callWaitingMessSuccess('Xóa sản phẩm thành công...', 'reload', true);
                }
                else {
                    jWaiting.callWaitingMessError(data.Message);
                }
            }
        });
    }
</script>